package com.itbupt.eat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itbupt.eat.common.BaseContext;
import com.itbupt.eat.entity.ShoppingCart;
import com.itbupt.eat.mapper.ShoppingCartMapper;
import com.itbupt.eat.service.IShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author BUPT-Dingchang
 * @Create 2024/5/2 0:58
 * ClassName: ShoppingCartServiceImpl
 * Description:
 */

@Service
@Slf4j
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements IShoppingCartService {

    /**
     * 添加菜品到购物车
     * @param shoppingCart  购物车对象
     * @return  购物车对象
     */
    @Override
    public ShoppingCart add(ShoppingCart shoppingCart) {

        // 获取当前登录用户的ID
        Long userId = BaseContext.getCurrentUserId();
        // 给传入的购物车菜品设置用户id
        shoppingCart.setUserId(userId);

        // 查询是否是首次添加
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, userId);
        if(shoppingCart.getDishId() != null) {
             // 用户添加的是菜品，添加菜品ID作为查询条件
            wrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {
            // 用户添加的是套餐，添加套餐ID作为查询条件
            wrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        // 调用数据层查询购物车
        ShoppingCart cart = this.getOne(wrapper);

        // 如果查询结果为空，则为首次添加--把number字段设为1
        if(cart == null) {
            shoppingCart.setNumber(1);
            this.save(shoppingCart);
            return shoppingCart;
        }

        // 不是的首次添加---number字段加1
        cart.setNumber(cart.getNumber() + 1);

        // 更新
        this.updateById(cart);
        return cart;
    }


    /**
     * 查询当前用户的购物车中所有信息
     * @return 购物车信息
     */
    @Override
    public List<ShoppingCart> getUserList() {
        // 获取当前登录用户的ID
        Long userId = BaseContext.getCurrentUserId();

        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(userId != null, ShoppingCart::getUserId, userId);
        wrapper.orderByAsc(ShoppingCart::getCreateTime);

        // 查询当前用户的购物车中所有信息
        return this.list(wrapper);

    }


    /**
     * 清空购物车
     * @return  是否清空成功
     */
    @Override
    public Boolean clean() {

        Long userId = BaseContext.getCurrentUserId();

        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(userId != null, ShoppingCart::getUserId, userId);

        return this.remove(wrapper);
    }

    @Override
    public Boolean sub(ShoppingCart shoppingCart) {
        // 获取当前登录用户的ID
        Long userId = BaseContext.getCurrentUserId();

        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(userId != null, ShoppingCart::getUserId, userId);

        // 判断传的是套餐还是菜品
        if(shoppingCart.getDishId() != null) {
            wrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {
            wrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        // 查询当前购物车里面该菜品/套餐的数量
        ShoppingCart shoppingCartSel = this.getOne(wrapper);
        if(1 < shoppingCartSel.getNumber()) {
            // 数量大于一，直接减一
            shoppingCartSel.setNumber(shoppingCartSel.getNumber() - 1);
            // 更新到数据库
            return this.updateById(shoppingCartSel);
        }

        // 5.其他情况直接从购物车删除此商品
        return this.remove(wrapper);
    }
}
